#include "FileInfoarea.h"
#include <QVBoxLayout>

FileInfoArea::FileInfoArea(QWidget *parent) : QWidget(parent)
{
    m_resizeTimer = new QTimer(this);
    m_resizeTimer->setSingleShot(true);
    connect(m_resizeTimer, &QTimer::timeout, this, &FileInfoArea::delayedResize);
    
    setupUI();
}

void FileInfoArea::setupUI()
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(10);
    mainLayout->setContentsMargins(5, 5, 5, 5);  // 减小边距

    // 图片显示区域
    m_scrollArea = new QScrollArea(this);
    m_scrollArea->setWidgetResizable(true);
    m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    m_scrollArea->setMinimumHeight(300);  // 设置最小高度
    
    m_imageLabel = new QLabel(this);
    m_imageLabel->setAlignment(Qt::AlignCenter);
    m_scrollArea->setWidget(m_imageLabel);
    
    // 增加图片区域的比例
    mainLayout->addWidget(m_scrollArea, 2);  // 增加权重

    // 文本信息区域
    m_infoTextEdit = new QTextEdit(this);
    m_infoTextEdit->setReadOnly(true);
    mainLayout->addWidget(m_infoTextEdit, 1);  // 减小权重
}

void FileInfoArea::displayFileInfo(const FileInfo& fileInfo)
{
    // 先清除当前显示
    m_imageLabel->clear();
    
    // 加载新图片
    QPixmap newPixmap(fileInfo.filePath);
    if (!newPixmap.isNull()) {
        m_currentPixmap = newPixmap;
        m_imageLabel->setScaledContents(false);  // 禁用自动缩放
        
        // 立即更新图片大小和显示
        updateImageSize();
    } else {
        m_currentPixmap = QPixmap();
        m_imageLabel->setText("无法加载图片");
    }

    // 更新文本信息
    QString infoText = QString("文件路径: %1\n").arg(fileInfo.filePath);
    infoText += QString("人名: %1\n").arg(fileInfo.peopleNames.join(", "));
    infoText += QString("内容类型: %1\n").arg(fileInfo.contentType);
    infoText += QString("内容描述: %1").arg(fileInfo.contentDescription);

    m_infoTextEdit->setText(infoText);
}

void FileInfoArea::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    // 使用较短的延迟时间
    m_resizeTimer->start(50);
}

void FileInfoArea::delayedResize()
{
    updateImageSize();
}

void FileInfoArea::updateImageSize()
{
    if (m_currentPixmap.isNull()) {
        return;
    }

    // 计算可用空间，留出边距
    int availableWidth = m_scrollArea->viewport()->width() - 20;
    int availableHeight = m_scrollArea->viewport()->height() - 20;
    
    // 获取原始图片尺寸
    QSize originalSize = m_currentPixmap.size();
    
    // 计算缩放比例
    double widthRatio = (double)availableWidth / originalSize.width();
    double heightRatio = (double)availableHeight / originalSize.height();
    
    // 选择较小的比例以适应视图
    double scaleFactor = qMin(widthRatio, heightRatio);
    
    // 如果图片比可用空间小，则保持原始大小
    if (scaleFactor > 1.0 && 
        originalSize.width() <= availableWidth && 
        originalSize.height() <= availableHeight) {
        m_imageLabel->setPixmap(m_currentPixmap);
        m_imageLabel->setFixedSize(originalSize);
    } else {
        // 计算新的尺寸，保持原始比例
        QSize newSize = originalSize * scaleFactor;
        
        // 使用高质量缩放
        QPixmap scaledPixmap = m_currentPixmap.scaled(
            newSize,
            Qt::KeepAspectRatio,
            Qt::SmoothTransformation
        );
        
        m_imageLabel->setPixmap(scaledPixmap);
        m_imageLabel->setFixedSize(newSize);
    }
    
    // 确保图片居中显示
    m_imageLabel->setAlignment(Qt::AlignCenter);
}
